package com.edu.order.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * 配置生效原则
 * 先导入优先、外部优先
 */
@Configuration
public class OrderApplicationRunner {

    /**
     * 1.项目启动就监听配置文件变化
     * 2.发生变化后拿到变化值
     * 3.发送邮件
     */
    @Bean
    ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager) {
        return args -> {
            ConfigService service = nacosConfigManager.getConfigService();
            service.addListener("service-order.properties", "DEFAULT_GROUP", new Listener() {
                @Override
                public Executor getExecutor() {
                    return Executors.newFixedThreadPool(4);
                }

                @Override
                public void receiveConfigInfo(String s) {
                    System.out.println("发生变化的配置信息 = " + s);
                }
            });
        };
    }
}
